package com.ginger.es.controller;

import com.ginger.es.dao.ItemRepository;
import com.ginger.es.model.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 资源控制器
 * 用来演示不同角色访问该控制器的接口返回的信息
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    ItemRepository itemRepository;
    /**
     * http://localhost:8080/test/add
     * @return 添加索引
     */
    @GetMapping("/add")
    public String add() throws Exception {
        Item item = new Item(1L, "小米手机7", " 手机",
                "小米", 3499.00, "http://image.baidu.com/13123.jpg");
        itemRepository.save(item);
        return "success";
    }

    @GetMapping("/insertList")
    public void insertList() {
        List<Item> list = new ArrayList<>();
        list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.baidu.com/13123.jpg"));
        list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.baidu.com/13123.jpg"));
        // 接收对象集合，实现批量新增
        itemRepository.saveAll(list);
    }

}